<template>
    <el-container>
        <el-header :height="'auto'" class="mb20">
            <breadcrumb :title="'IP实施工单'" :noBack="true"></breadcrumb>
        </el-header>
        <el-main>
            <el-container class="white-block box full" v-loading="loading">
                <el-header :height="'auto'">
                    <div class="toolbar">
                        <div>
                            <el-input v-model="appName" placeholder="请输入自建系统名称" size="small"
                                      class="input"></el-input>
                            <el-input v-model="ip" placeholder="请输入IP地址" size="small"
                                      class="input"></el-input>
                            <el-button class="primary" size="small" @click="getList(1)">查询</el-button>
                            <el-button class="minor" @click="reset()" size="small">重置</el-button>
                        </div>
                    </div>
                </el-header>
                <el-main class="mt10">
                    <table-example :data="tableData" :tableColumn="header" :slotColumns="slotColumns" :height="'100%'"
                                   ref="table">
                        <template slot="state" slot-scope="scope">
                            <span v-if="scope.row.state==0">待实施</span>
                            <span v-else-if="scope.row.state==1">已完成</span>
                        </template>
                        <template slot="operation" slot-scope="scope">
                            <div class="operation">
                                <el-link type="primary" :underline="false" @click="change(scope.row)"
                                         v-if="scope.row.state==0">完成
                                </el-link>
                                <el-link type="primary" :underline="false" @click="openDetail(scope.row)">查看</el-link>
                            </div>
                        </template>
                        <template slot="oldIP" slot-scope="scope">
                            <template v-if="scope.row.oldIP">
                                <p v-for="(ip, index) in scope.row.oldIP.split(',')" :key="index">
                                    {{ip}}
                                </p>
                            </template>
                        </template>
                    </table-example>
                </el-main>
                <el-footer :height="'auto'" class="mt20">
                    <page :data="page" v-if="page" class="page" @jumpPage="getList"></page>
                </el-footer>
            </el-container>
            <transition name="slide">
                <router-view @upload="getList"></router-view>
            </transition>
        </el-main>
    </el-container>
</template>

<script>
    import breadcrumb from '../../components/breadcrumb'
    import tableExample from '../../components/table'
    import page from '../../components/pagination'
    import dialogBox from '../../components/dialog'
    import selectTree from '../../components/select-tree'

    export default {

        data() {
            return {
                tableData: [],
                header: [
                    {
                        prop: 'orderNo',
                        label: '实施工单号',
                        minWidth: '130',
                        align: 'center'
                    },
                    {
                        prop: 'sysName',
                        label: '自建系统名称',
                        minWidth: '180',
                        align: 'center'
                    },
                    {
                        prop: 'oldIP',
                        label: 'IP',
                        minWidth: '150',
                        align: 'center'
                    },
                    {
                        prop: 'userName',
                        label: '申请人',
                        minWidth: '150',
                        align: 'center'
                    },
                    {
                        prop: 'createTime',
                        label: '申请时间',
                        minWidth: '150',
                        align: 'center'
                    },
                    {
                        prop: 'state',
                        label: '实施状态',
                        minWidth: '100',
                        align: 'center'
                    },
                    {
                        prop: 'operation',
                        label: '操作',
                        align: 'center',
                        minWidth: '120',
                        fixed: 'right'
                    }
                ],
                slotColumns: ['operation', 'state', 'oldIP'],
                page: {},
                loading: false,
                index: 1,
                appName: '',
                ip: '',
                currentList: null
            }
        },
        mounted() {
            this.getList()
        },
        methods: {
            validate(formName, callback) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        if (callback) callback()
                    } else {
                        return false
                    }
                })
            },
            getList(index) {
                this.loading = true
                if (index) this.index = index
                this.$http.get('/rdIpImpleOrder/ipImplementationOrderPageList', {
                    current: this.index || 1,
                    size: 15,
                    appName: this.appName,
                    ip: this.ip
                }).then(result => {
                    this.loading = false
                    if (result.success) {
                        this.tableData = result.data.records
                        this.page = result.data
                        delete this.page['records']
                    }
                }).catch(() => {
                    this.loading = false
                })
            },
            reset() {
                this.appName = ''
                this.ip = ''
                this.getList(1)
            },
            change(row) {
                let _this = this
                _this.loading = true
                this.$confirm('确定实施已完成吗？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    implDone(row)
                }).catch(() => {
                    _this.loading = false
                })

                function implDone(row) {
                    _this.$http.get('/rdIpImpleOrder/implementationDone', {
                        orderId: row.id
                    }).then(result => {
                        _this.loading = false
                        if (result.success) {
                            _this.$message.success({
                                message: '实施完成成功',
                                showClose: true
                            })
                            _this.getList()
                        }
                    })
                }
            },
            // 打开查看申请单详情页面
            openDetail(row) {
                this.$router.push({
                    path: this.$route.path + '/detail',
                    query: {
                        id: row.id
                    }
                })
            }
        },
        components: {
            tableExample,
            breadcrumb,
            page,
            dialogBox,
            selectTree
        }
    }
</script>

<style scoped lang="less">

</style>
